package com.example.pei.ithinklearnt.view.fragments.innerfragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.adapter.UserSkillAdapter;
import com.example.pei.ithinklearnt.bean.UserInfo;
import com.example.pei.ithinklearnt.present.DetailedInformationPresent;
import com.example.pei.ithinklearnt.present.DetailedInformationPresentImp;
import com.example.pei.ithinklearnt.view.customview.EmptyRecyclerView;
import com.example.pei.ithinklearnt.view.interfaces.DetailedInformationInterface;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;

/**
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *            佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
*/

//个人的技能卡
public class DetailedInformationFragment extends Fragment implements DetailedInformationInterface{

    private DetailedInformationPresent detailedInformationPresent;


    private UserSkillAdapter adapter;
    private String[] weakSkills;
    private String[] strongSkills;

    public final static int WEAKSKILLCODE=98;
    public final static int STRONGSKILLCODE=99;

    public final static int WEAKMAXNUMBER=3;
    public final static int STRONGMAXNUMBER=8;


    @BindView(R.id.skillrecyclerview)
    RecyclerView mRecycleView;

    private UserInfo displayUser;

    public static final DetailedInformationFragment newInstance(UserInfo userInfo)
    {
        DetailedInformationFragment fragment = new DetailedInformationFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("u",userInfo);
        fragment.setArguments(bundle);

        return fragment ;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments()!=null) {
            displayUser = (UserInfo) getArguments().getSerializable("u");
        }
    }


    // TODO: 2018/1/22 只有一次requestUserSkills ，如果获取失败怎么把办
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detailedinformation_fragment, container, false);
        ButterKnife.bind(this, view);
        detailedInformationPresent =new DetailedInformationPresentImp(this);
        if(displayUser!=null){
            boolean isCurrentUser;  //判断展示的用户是不是当前用户 true 是
            int sex=0;
            if(AppApplication.getCurrentUser().getObjectId().equals(displayUser.getObjectId())){
                isCurrentUser=true;
            }else {
                sex=displayUser.getSex();
                isCurrentUser=false;
            }
            initView(isCurrentUser,sex);
            detailedInformationPresent.requestUserSkills(displayUser,isCurrentUser);
        }
        return view;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }else {
            List<String> skills=data.getStringArrayListExtra("skills");
            String[] stringSkills = (String[])skills.toArray(new String[skills.size()]);
            switch (requestCode){
                case WEAKSKILLCODE:{
                    bindingSkills(stringSkills,null);
                    break;
                }
                case STRONGSKILLCODE:{
                    bindingSkills(null,stringSkills);
                    break;
                }
            }
        }
    }


    @Override
    public void bindingSkills(String[] weakSkills,String[] strongSkills) {
        adapter.setSkills(weakSkills,strongSkills);
        if(weakSkills!=null){
            this.weakSkills=weakSkills;
        }
        if(strongSkills!=null){
            this.strongSkills=strongSkills;
        }

        adapter.notifyDataSetChanged();
    }


    private void initView(boolean isCurrentUser,int sex) {
// TODO: 2018/1/22  我想学的技能在我会里面是选不了的，两者应当互斥
        adapter = new UserSkillAdapter(getContext(), isCurrentUser,sex,new UserSkillAdapter.UserSkillListener() {
            @Override
            public void chooseWeakSkills() {
                Intent intent=new Intent("ChooseSkill_Activity");
                intent.putExtra("type",WEAKMAXNUMBER);
                ArrayList<String> selectedSkills=new ArrayList<>();//将现在选中的技能也传到活动里
                ArrayList<String> hideSkills=new ArrayList<>();    //需要隐藏的技能
                Collections.addAll(selectedSkills, weakSkills);
                Collections.addAll(hideSkills, strongSkills);
                intent.putStringArrayListExtra("selectedSkills",selectedSkills);
                intent.putStringArrayListExtra("hideSkills",hideSkills);
                startActivityForResult(intent,WEAKSKILLCODE);
            }

            @Override
            public void chooseStrongSkills() {
                Intent intent=new Intent("ChooseSkill_Activity");
                intent.putExtra("type",STRONGMAXNUMBER);
                ArrayList<String> selectedSkills=new ArrayList<>();//将现在选中的技能也传到活动里
                ArrayList<String> hideSkills=new ArrayList<>();    //需要隐藏的技能
                Collections.addAll(selectedSkills, strongSkills);
                Collections.addAll(hideSkills,weakSkills );
                intent.putStringArrayListExtra("selectedSkills",selectedSkills);
                intent.putStringArrayListExtra("hideSkills",hideSkills);
                startActivityForResult(intent,STRONGSKILLCODE);
            }
        });
        mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecycleView.setAdapter(adapter);
    }




}
